<template>
  <div class="admin-order-list">
    <a-card :bordered="false">
      <!-- 搜索区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单号">
                <a-input v-model="queryParam.keyword" placeholder="请输入订单号" allowClear />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => { this.queryParam = {} }">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- 表格 -->
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="false"
        rowKey="id"
        :scroll="{ x: 1500 }"
      >
        <span slot="status" slot-scope="text">
          <a-tag :color="getStatusColor(text)">{{ getStatusText(text) }}</a-tag>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleView(record)">查看</a>
            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">
                更多 <a-icon type="down" />
              </a>
              <a-menu slot="overlay">
                <a-menu-item v-if="record.status === 'PENDING'">
                  <a @click="handleUpdateStatus(record.id, 'PAID')">标记为已付款</a>
                </a-menu-item>
                <a-menu-item v-if="record.status === 'PAID'">
                  <a @click="handleUpdateStatus(record.id, 'SHIPPED')">标记为已发货</a>
                </a-menu-item>
                <a-menu-item v-if="record.status === 'SHIPPED'">
                  <a @click="handleUpdateStatus(record.id, 'COMPLETED')">标记为已完成</a>
                </a-menu-item>
                <a-menu-item v-if="record.status === 'PENDING'">
                  <a @click="handleUpdateStatus(record.id, 'CANCELLED')">取消订单</a>
                </a-menu-item>
                <a-menu-item v-if="record.status === 'CANCELLED' || record.status === 'COMPLETED'">
                  <a @click="handleDelete(record)">删除</a>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </template>
        </span>
      </s-table>

      <!-- 查看订单详情的抽屉 -->
      <a-drawer
        title="订单详情"
        placement="right"
        :width="720"
        :visible="visible"
        :after-visible-change="afterVisibleChange"
        @close="onClose"
      >
        <template v-if="currentOrder">
          <a-descriptions title="订单信息" :column="2">
            <a-descriptions-item label="订单号">{{ currentOrder.orderNo }}</a-descriptions-item>
            <a-descriptions-item label="订单状态">
              <a-tag :color="getStatusColor(currentOrder.status)">
                {{ getStatusText(currentOrder.status) }}
              </a-tag>
            </a-descriptions-item>
            <a-descriptions-item label="下单用户">{{ currentOrder.username }}</a-descriptions-item>
            <a-descriptions-item label="订单金额">¥{{ currentOrder.totalAmount }}</a-descriptions-item>
            <a-descriptions-item label="收货人">{{ currentOrder.shippingName }}</a-descriptions-item>
            <a-descriptions-item label="联系电话">{{ currentOrder.shippingPhone }}</a-descriptions-item>
            <a-descriptions-item label="收货地址" :span="2">{{ currentOrder.shippingAddress }}</a-descriptions-item>
            <a-descriptions-item label="创建时间">{{ currentOrder.createdTime }}</a-descriptions-item>
            <a-descriptions-item label="更新时间">{{ currentOrder.updatedTime }}</a-descriptions-item>
          </a-descriptions>

          <a-divider />

          <a-descriptions title="商品信息">
            <a-table
              :columns="itemColumns"
              :data-source="currentOrder.orderItems"
              :pagination="false"
              :rowKey="record => record.id"
            >
              <template slot="productImage" slot-scope="text">
                <img :src="text" alt="商品图片" style="width: 50px; height: 50px;" />
              </template>
            </a-table>
          </a-descriptions>
        </template>
      </a-drawer>
    </a-card>
  </div>
</template>

<script>
import { STable } from '@/components'
import { getOrderList, getOrderDetail, updateOrderStatus, deleteOrder } from '@/api/order'

export default {
  name: 'AdminOrderList',
  components: {
    STable
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 表格列定义
      columns: [
        {
          title: '订单号',
          dataIndex: 'orderNo',
          width: 180
        },
        {
          title: '用户名',
          dataIndex: 'username',
          width: 100
        },
        {
          title: '订单金额',
          dataIndex: 'totalAmount',
          width: 100,
          customRender: (text) => `¥${text}`
        },
        {
          title: '订单状态',
          dataIndex: 'status',
          width: 100,
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '收货人',
          dataIndex: 'shippingName',
          width: 100
        },
        {
          title: '联系电话',
          dataIndex: 'shippingPhone',
          width: 120
        },
        {
          title: '收货地址',
          dataIndex: 'shippingAddress',
          width: 200
        },
        {
          title: '创建时间',
          dataIndex: 'createdTime',
          width: 160,
          sorter: true
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 150,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 订单项表格列定义
      itemColumns: [
        {
          title: '商品图片',
          dataIndex: 'productImage',
          width: 80,
          scopedSlots: { customRender: 'productImage' }
        },
        {
          title: '商品名称',
          dataIndex: 'productName',
          width: 200
        },
        {
          title: '单价',
          dataIndex: 'price',
          width: 100,
          customRender: (text) => `¥${text}`
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          width: 80
        },
        {
          title: '小计',
          dataIndex: 'subtotal',
          width: 100,
          customRender: (text) => `¥${text}`
        }
      ],
      // 抽屉显示控制
      visible: false,
      // 当前查看的订单
      currentOrder: null
    }
  },
  methods: {
    // 加载表格数据
    loadData (parameter) {
      const requestParameters = Object.assign({}, parameter, this.queryParam)
      return getOrderList(requestParameters)
        .then(res => {
          return res.data
        })
    },

    // 获取状态显示文本
    getStatusText (status) {
      const statusMap = {
        PENDING: '待付款',
        PAID: '已付款',
        SHIPPED: '已发货',
        COMPLETED: '已完成',
        CANCELLED: '已取消'
      }
      return statusMap[status] || status
    },

    // 获取状态标签颜色
    getStatusColor (status) {
      const colorMap = {
        PENDING: 'orange',
        PAID: 'green',
        SHIPPED: 'blue',
        COMPLETED: 'green',
        CANCELLED: 'red'
      }
      return colorMap[status] || 'default'
    },

    // 查看订单详情
    handleView (record) {
      getOrderDetail(record.id).then(res => {
        this.currentOrder = res.data
        this.visible = true
      })
    },

    // 更新订单状态
    handleUpdateStatus (id, status) {
      const that = this
      this.$confirm({
        title: '确认操作',
        content: `确定要将订单状态更新为"${this.getStatusText(status)}"吗？`,
        onOk () {
          return updateOrderStatus(id, status).then(() => {
            that.$message.success('更新成功')
            that.$refs.table.refresh()
          })
        }
      })
    },

    // 删除订单
    handleDelete (record) {
      const that = this
      this.$confirm({
        title: '确认删除',
        content: '确定要删除该订单吗？',
        onOk () {
          return deleteOrder(record.id).then(() => {
            that.$message.success('删除成功')
            that.$refs.table.refresh()
          })
        }
      })
    },

    afterVisibleChange (val) {
      console.log('visible', val)
    },

    onClose () {
      this.visible = false
    }
  }
}
</script>

<style lang="less" scoped>
.admin-order-list {
  .table-page-search-wrapper {
    margin-bottom: 16px;
  }
}
</style> 